home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZReferenceCounted.h < prev    next >
Text File  |  1996-10-28  |  2KB  |  87 lines

  1. /*
  2.  *  File:       ZReferenceCounted.h
  3.  *  Summary:    Mixin class to help with reference counting.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *    Abstract:    Reference counting is basicly a weak form of garbage collection: it allows 
  10.  *                one object to be shared by other objects. When there are no more references 
  11.  *                to the first object it is automatically deleted.
  12.  *
  13.  *                Using reference counting reduces memory requirements and also always heavy 
  14.  *                weight objects to be quickly copied.
  15.  *
  16.  *    Usage:        Reference counting is typically implemented using the envelope/letter pattern. 
  17.  *                The envelope is the class users see. The envelope has a pointer to the letter 
  18.  *                which does all the work. Letters can belong to more than one envelope. Here's 
  19.  *                an example of how this works:
  20.  *
  21.  *                TString::~TString() 
  22.  *                {
  23.  *                    mRefPtr->RemoveReference();            // when refCount goes to zero letter deletes itself
  24.  *                }
  25.  *
  26.  *                TString::TString(const char* str) 
  27.  *                {
  28.  *                    mRefPtr = new TStringRef(str);
  29.  *                }
  30.  *
  31.  *                TString::TString(const TString& rt) 
  32.  *                {
  33.  *                    mRefPtr = rt.mRefPtr;                 // note that the pointer is copied -- not the data!
  34.  *                    mRefPtr->AddReference();
  35.  *                }
  36.  *
  37.  *                TString& TString::operator=(const TString& rt)
  38.  *                {
  39.  *                    if (mRefPtr != rt.mRefPtr) {
  40.  *                        mRefPtr->RemoveReference();        
  41.  *
  42.  *                        mRefPtr = rt.mRefPtr;
  43.  *                        mRefPtr->AddReference();
  44.  *                    }
  45.  *                    return *this;
  46.  *                }
  47.  *
  48.  *  Change History (most recent first):    
  49.  *
  50.  *         <->     1/14/96    JDJ        Created.
  51.  */
  52.  
  53. #pragma once
  54.  
  55. #include <ZDebug.h>
  56. #include <ZTypes.h>
  57.  
  58.  
  59. // ===================================================================================
  60. //    class MReferenceCounted
  61. // ===================================================================================
  62. class MReferenceCounted {
  63.  
  64. public:
  65.     virtual             ~MReferenceCounted() = 0;
  66.  
  67.     explicit             MReferenceCounted(long refCount = 1);
  68.     
  69.  
  70.     virtual void         AddReference();
  71.             
  72.     virtual void         RemoveReference();
  73.                         // Deletes the object if no one is referencing it.
  74.             
  75.             long         GetRefCount() const                    {return mRefCount;}
  76.  
  77. protected:
  78.     virtual void         OnFirstReference();
  79.                         // Defaults to nothing.
  80.     
  81.     virtual void         OnLastReference();
  82.                         // Defaults to deleting the object.
  83.  
  84. private:
  85.     long    mRefCount;
  86. };
  87.